#!/bin/bash

# 定义nacos接口信息
NACOS_URL="http://localhost:8848/nacos/v1/ns/service"  
USERNAME="nacos"  
PASSWORD="nacos"  
SERVICE_NAME="ruoyi-gateway"  
GROUP_NAME="DEFAULT_GROUP"  
NAMESPACE_ID="rhhf" 

# 前置触发条件1
if [ 403 != $(curl -o /dev/null -s -w "%{http_code}\n" http://127.0.0.1/api/auth/getLoginUser) ];then
    # 前置触发条件2
    if [ 1 == $(/usr/bin/docker ps |grep service-ruoyi-gateway-svc |grep healthy |wc -l) ];then

        # 发送请求，并将响应存储在变量中  
        response=$(curl -s -u $USERNAME:$PASSWORD "$NACOS_URL?serviceName=$SERVICE_NAME&groupName=$GROUP_NAME&namespaceId=$NAMESPACE_ID")  

        # 检查返回的 clusters 数组  
        if echo "$response" | grep -q '"clusters":\s*\[\]'; then  
            echo "错误: nacos没有可用的集群实例, gateway异常假死"
            service docker restart
        else  
            echo "正确：服务实例信息如下："
            echo "$response"  
        fi

    fi

fi

#echo "*/5 * * * * root /bin/sh /sumz/if_docker_restart.sh" >> /etc/crontab


